<template>
	<view class="content">
		<image class="loginback" src="/static/index/loginback.png" @click="back"></image>
		<view class="di"></view>
			<view class="tou">
				<view class="title">请输入验证码</view>
				<view class="ts">验证码已发送至{{phone}}</view>
				<view class="input-box">
					<input class="inputa" maxlength="1" type="number" :focus="focus1" v-model="code1" @input="code1Change"/>
					<input class="inputa" maxlength="1" type="number" :focus="focus2" v-model="code2" @input="code2Change"/>
					<input class="inputa" maxlength="1" type="number" :focus="focus3" v-model="code3" @input="code3Change"/>
					<input class="inputa" maxlength="1" type="number" :focus="focus4" v-model="code4" @input="code4Change"/>
				</view>
				<view class="cxhq" v-if="time>0">{{time}}后重新获取</view>
				<view class="cxhqa" v-if="time==0" @click="getCode">重新获取</view>
				<button class="btn1" :disabled="showbtnDisabled" @click="goLogin">同意协议并登录</button>
				<view class="xy" @click="choStatus=!choStatus">
					<image :src="choStatus?'/static/xz.png':'/static/ysxyx.png'"></image>
					已同意并阅读<text @click.stop="goUrl('https://home.miaokaikeji.com/web/agreement/serviceAgreement.html')">《用户服务协议》</text>和<text @click.stop="goUrl('https://home.miaokaikeji.com/web/agreement/privacyPolicy.html')">《隐私政策》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				phone:'',
				focus1:true,
				focus2:false,
				focus3:false,
				focus4:false,
				code1:'',
				code2:'',
				code3:'',
				code4:'',
				time:60,
				choStatus:false,
			};
		},
		onLoad(options) {
			_this=this;
			if(options.phone){
				_this.phone=options.phone;
			}
			_this.timeout();
		},
		computed:{
			showbtnDisabled :function(){
				if(this.code1 && this.code2 && this.code3 && this.code4){
					return false;
				}else{
					return true;
				}
			}
		},
		methods:{
			back(){
				_this.$routerTo.navigateBack();
			},
			goUrl(url){
				uni.navigateTo({
					url:'/pages/index/webview?url='+url
				})
			},
			// 倒计时时间
			timeout(){
				let timeer=setInterval(function(){
					if(_this.time==0){
						clearInterval(timeer)
						_this.time=0;
						return;
					}
					_this.time--;
				},1000)
			},
			code1Change(event){
				if(event.detail.value.length==1){
					_this.focus1=false;
					_this.focus2=true;
				}
			},
			code2Change(event){
				if(event.detail.value.length==1){
					_this.focus2=false;
					_this.focus3=true;
				}
			},
			code3Change(event){
				if(event.detail.value.length==1){
					_this.focus3=false;
					_this.focus4=true;
				}
			},
			code4Change(event){
				if(event.detail.value.length==1){
					_this.focus4=false;
				}
			},
			getCode(){
				_this.$HTTP({
					method: 'GET',
					url: 'shopServer/user/getCode/'+_this.phone,
					data: {
						time: Date.parse(new Date())
					}
					// header:'form'
				}).then(res => {
					_this.$api.msg('验证码发送成功！');
				}).catch(err => {
					console.log(err);
				});
			},
			goLogin(){
				if(!_this.choStatus){
					_this.$api.msg('请同意勾选用户服务协议');
					return;
				}
				let newCode=_this.code1+''+_this.code2+''+_this.code3+''+_this.code4+'';
				_this.$HTTP({
					method: 'POST',
					url: 'shopServer/user/loginOrRegister',
					data: {
						code: newCode,
						phone: _this.phone,
						channel: 1,
						agentId: _this.$cookie.get('agentId')?_this.$cookie.get('agentId'):'',
						time: Date.parse(new Date())
					},
					header:'form'
				}).then(res => {
					_this.$cookie.remove('agentId');
					_this.$cookie.set('token',res.token);
					// 储存数据
					let userData={};
					let { headUrl, nickname, openId, id, anchor } = res;
					userData = Object.assign(userData, {
					    headUrl,
					    nickname,
						openId,
						id,
						anchor
					});
					_this.$cookie.set('userInfo',userData);
					// 跳转
					_this.$routerTo.reLaunch({
						path: '/pages/index/index'
					});
				}).catch(err => {
					console.log(err);
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.xy{
			width: 110%;
			font-size: 26rpx;
			color: #999999;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			margin-top: 44rpx;
			transform: translateX(-15rpx);
			text{
				color: #333333;
			}
			image{
				width: 30rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}
		}
.content {
	width: 100%;
	height: 100vh;
	background-image: url('https://home.miaokaikeji.com/web/applet/newimg/loginbg.png');
	background-size: 100% 100%;
	position: relative;
	.loginback{
		width: 48rpx;
		height: 48rpx;
		position: absolute;
		left: 40rpx;
		top: 110rpx;
	}
	.di{
		width: 646rpx;
		height: 58vh;
		background: #FFFFFF;
		border-radius: 40rpx;
		opacity: 0.63;
		position: absolute;
		left: 52rpx;
		top: 26vh;
		z-index: 1;
	}
	.tou{
		width: 686rpx;
		height: 60vh;
		background: #FFFFFF;
		border-radius: 40rpx;
		position: absolute;
		left: 32rpx;
		top: 22vh;
		z-index: 2;
		box-sizing: border-box;
		padding: 72rpx 32rpx 0 32rpx;
		.title{
			width: 100%;
			text-align: center;
			font-size: 48rpx;
			color: #333333;
		}
		.ts{
			width: 100%;
			text-align: center;
			font-size: 28rpx;
			color: #999999;
			margin-top: 10rpx;
		}
		.cxhq{
			width: 100%;
			margin-top: 36rpx;
			font-size: 28rpx;
			color: #999999;
		}
		.cxhqa{
			width: 100%;
			margin-top: 36rpx;
			font-size: 28rpx;
			color: #FF9416;
		}
		.input-box{
			width: 100%;
			margin-top: 110rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.inputa{
				width: 120rpx;
				height: 110rpx;
				font-size: 40rpx;
				background: #F2F0EE;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				text-align: center;
			}
		}
		.btn1 {
			width: 100%;
			height: 96rpx;
			background: #FF9416;
			border-radius: 59rpx;
			line-height: 96rpx;
			color: white;
			font-size: 36rpx;
			margin-top: 60rpx;
		}
		.btn1[disabled]{
			background: #CCCCCC !important;
		}
	}
}
</style>
